//
//  HYHomeVideoListCell.m
//  XPlayer
//
//  Created by liguoqing on 2023/8/9.
//

#import "HYHomeVideoListCell.h"
#import <HYUtls/CommUtls+Time.h>
#import <HYUtls/CommUtls+NSInteger.h>

@interface HYHomeVideoListCell ()

@property (nonatomic, strong) UIImageView *playLogo;
// 视频时长
@property (nonatomic, strong) UILabel *durationLabel;
// 观看进度
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation HYHomeVideoListCell

- (void)configUI
{
    [super configUI];
    self.coverView.contentMode = UIViewContentModeScaleAspectFill;
    [self.backView addSubview:self.coverView];
    [self.coverView addSubview:self.progressView];
    [self.backView addSubview:self.iconView];
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.playLogo];
    [self.backView addSubview:self.durationLabel];
    [self.backView addSubview:self.dateLabel];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.backView);
        make.width.mas_equalTo(kCoverWidth);
    }];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.coverView);
        make.height.mas_equalTo(2);
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.coverView);
        make.width.height.mas_equalTo(24);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverView.mas_top).offset(5);
        make.left.equalTo(self.coverView.mas_right).offset(10);
        make.right.equalTo(self.backView).offset(-50);
    }];
    [self.playLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.coverView.mas_centerY);
        make.left.equalTo(self.titleLabel.mas_left);
        make.width.height.mas_equalTo(24);
    }];
    [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.playLogo.mas_right).offset(5);
        make.centerY.equalTo(self.playLogo.mas_centerY);
        make.height.mas_equalTo(15);
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.coverView.mas_bottom).offset(-5);
        make.left.equalTo(self.titleLabel.mas_left);
        make.right.equalTo(self.contentView).offset(-44);
    }];
    [self layoutNoneSetBtn];
}

- (void)layoutNoneSetBtn
{
    NSLog(@"layoutNoneSetBtn");
}

- (void)bindViewModel:(HYHomeFileItemViewModel *)vm
{
    [super bindViewModel:vm];
    if(vm.isLock){
        self.iconView.hidden = NO;
        self.iconView.image = [UIImage imageNamed:@"icon_lock_mode"];
        self.coverView.image = [UIImage imageNamed:@""];
        self.coverView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#F5F5F5" darkModeHex:@"#404246"];
    }else{
        if (vm.fileType == HYHomeFileType_Video) {
            self.iconView.hidden = YES;
            if(vm.videoImage){
                self.coverView.image = vm.videoImage;
            }else{
                if(vm.fileImage){
                    vm.videoImage = [UIImage imageWithContentsOfFile:[HYFileManager videoImageSavePathWithFileName:vm.fileImage]];
                    self.coverView.image = vm.videoImage;
                }else{
                    dispatch_async(dispatch_get_global_queue(0, 0), ^{
                        UIImage *coverImage = [CommUtls fetchVideoCoverImageWithUrl:vm.filePath atTime:1.0];
                        if(coverImage){
                            dispatch_async(dispatch_get_main_queue(), ^{
                                vm.videoImage = coverImage;
                                self.coverView.image = vm.videoImage;
                            });
                        }else{
                            dispatch_async(dispatch_get_main_queue(), ^{
                                self.iconView.hidden = NO;
                                self.coverView.image = nil;
                                self.coverView.backgroundColor = APPMainThemeColor;
                                self.iconView.image = [UIImage imageNamed:@"icon_video"];
                            });
                        };
                    });
                }
            }
            self.coverView.backgroundColor = UIColor.clearColor;
        }else if (vm.fileType == HYHomeFileType_Audio){
            self.iconView.hidden = YES;
            self.coverView.image = [UIImage imageNamed:@"icon_file_music"];
        }
    }
    self.titleLabel.text = vm.fileName;
    CGFloat titleHeight = [CommUtls fetchTextHeightWithText:vm.fileName
                                                       font:FONT_BOLD_SIZE(14)
                                                 labelWidth:kScreenWidth-15-kCoverWidth-(vm.isHiddenSet?5:50)
                                                  maxHeight:34];
    if(titleHeight > 17){
        [self.playLogo mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.coverView.mas_centerY).offset(10);
        }];
    }else{
        [self.playLogo mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.coverView.mas_centerY);
        }];
    }
    if(vm.isHistory){
        self.progressView.hidden = NO;
        NSString *currentTime = [CommUtls timeToMinMS:@(vm.playTime)];
        self.durationLabel.text = [NSString stringWithFormat:@"%@/%@",currentTime,vm.videoDuration];
        NSInteger duration = [CommUtls hmsToTime:vm.videoDuration];
        CGFloat progress = vm.playTime*1./duration;
        self.progressView.progress = progress;
        self.dateLabel.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"上次播放时间：", nil),vm.lastTime];
    }else{
        self.progressView.hidden = YES;
        self.durationLabel.text = vm.videoDuration;
        if(vm.importDate){
            self.dateLabel.text = vm.importDate;
        }else if (vm.createDate){
            self.dateLabel.text = vm.createDate;
        }else if (vm.fileSize){
            self.dateLabel.text = [CommUtls getSize:@(vm.fileSize)];
        }
    }
}

#pragma mark - loadLazy
- (UIImageView *)playLogo
{
    if(!_playLogo){
        _playLogo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_play_light"]];
    }
    return _playLogo;
}

- (UILabel *)durationLabel
{
    if(!_durationLabel){
        _durationLabel = [[UILabel alloc]init];
        _durationLabel.textColor = UIColor.systemGrayColor;
        _durationLabel.font = kIsIPad?FONT_SIZE(16):FONT_SIZE(12);
    }
    return _durationLabel;
}

- (UIProgressView *)progressView
{
    if(!_progressView){
        _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.progressTintColor = APPMainThemeColor;
        _progressView.trackTintColor = [UIColor clearColor];
    }
    return _progressView;
}

@end
